package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.presenter.IPresenter;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.SRPUtils;
import com.tencent.cloud.uikit.ui.widget.IovToolbar;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.model.bind.PinSetFirstRequest;
import cn.sirun.com.fca.account.model.bind.PinSetFirstResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamResponse;
import cn.sirun.com.fca.account.model.bind.PinSetParamsRequest;
import cn.sirun.com.fca.account.presenter.ISettingNewPinPresent;
import cn.sirun.com.fca.account.presenter.impl.SettingNewPinPresenter;
import cn.sirun.com.fca.account.view.ISettingInitalPinView;

public class SettingInitalPinFlowView extends BaseLoadingFlowView<ISettingNewPinPresent> implements ISettingInitalPinView {


    private GridPasswordView mPinGPV;

    private String pin ="";

    public SettingInitalPinFlowView(Activity activity) {
        super(activity);
    }

    public SettingInitalPinFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    protected ISettingNewPinPresent createPresenter() {
        return new SettingNewPinPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        final IovToolbar toolbar = SystemUiUtils.showActionBar(
                activity, "");
        toolbar.setHomeAsUpText(R.string.tv_title_upback);

    }

    private void initView() {
        setContentView(R.layout.setting_inital_pin_activity);
        final Activity activity = getActivity();

        mPinGPV = activity.findViewById(R.id.gpv_pin);

        mPinGPV.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                pin = psw;

                PinSetParamsRequest request = new PinSetParamsRequest();
                getPresenter().getPinSetParams(request);
            }
        });


    }


    @Override
    public void getPinSetParams(BaseResponse<PinSetParamResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            /**
             * 获取到PIN设置信息，再请求设置PIN接口
             */
            PinSetFirstRequest request = new PinSetFirstRequest();
            if(response.getData().getSalt() != null && response.getData().getGroup() != null){
                String salt = response.getData().getSalt();
                String group = response.getData().getGroup();
                request.setPin(SRPUtils.encryptData(pin, salt, group));



            }
            getPresenter().pinSetFirst(request);
        }else {

        }
    }

    @Override
    public void pinSetFirst(BaseResponse<PinSetFirstResponse> result) {

    }

    @Override
    public void pinResetResult(BaseResponse<PinSetFirstResponse> result) {

    }
}
